/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ania2;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import ania2.draughtsboard.Board;
import ania2.enums.Fields;
import java.awt.Point;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

/**
 *
 * @author pawel
 */
public class BoardManager implements MouseListener {

    private Board board;
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    private Move lastMove;
    private List<Move> possibleMoves;
    private List<Move> possibleMovesFromSelectedField;
    private EnumSet<Fields> affectedFields;
    private int border;
    private int fieldWidth;

    public BoardManager(Board board, int border, int size) {
        this.board = board;
        possibleMovesFromSelectedField = new ArrayList<Move>();
        this.border = border;
        updateBoardSize(size);
        this.affectedFields = EnumSet.noneOf(Fields.class);
    }


    /**
     * Get the value of lastMove
     *
     * @return the value of lastMove
     */
    public Move getLastMove() {
        return lastMove;
    }

    /**
     * Set the value of lastMove
     *
     * @param lastMove new value of lastMove
     */
    public void setLastMove(Move lastMove) {
        this.lastMove = lastMove;
    }

   
    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    public void updateBoardSize(int size) {
        this.fieldWidth = (size - 2 * border) / 8;
    }

    public void setBorder(int border) {
        this.border = border;
    }

    public EnumSet<Fields> getAffectedFields() {
        return affectedFields;
    }

    public void setPossibleMoves(List<Move> possibleMoves) {
        this.possibleMoves = possibleMoves;
    }
    
    

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        if(!board.isEnabled())
            return;
        Point p = e.getPoint();
        int column = (int) Math.ceil((p.x - border) / fieldWidth);
        int row = (int) Math.ceil((p.y - border) / fieldWidth);
        String name = String.valueOf(Character.toChars('A' + column)) + (8 - row);
        try {
            Fields field = Fields.valueOf(name);
            if (affectedFields.size() < 2) {
                for (Move m : possibleMoves) {
                    if (m.getSourceField() == field) {
                        affectedFields.add(m.getTargetField());
                        possibleMovesFromSelectedField.add(m);
                    }
                }
                if(!affectedFields.isEmpty())
                    affectedFields.add(field);
                propertyChangeSupport.firePropertyChange("selectedField", false, true);
            }
            else {
                for(Move m : possibleMovesFromSelectedField) {
                    if(m.getTargetField() == field) {
                        lastMove = m;
                        propertyChangeSupport.firePropertyChange("moveExecuted", false, true);
                    }
                }
                possibleMovesFromSelectedField.clear();
                affectedFields.clear();
            }
        } catch (IllegalArgumentException ex) {
        } catch(NullPointerException ex) {
        }
    }

    public void mouseReleased(MouseEvent e) {
    }
}
